//
//  ShmDownloadTool.swift
//  mloader
//
//  Created by insbridge-macmini1 on 2022/4/29.
//

import UIKit
import MZDownloadManager
import SwiftyJSON

// MARK:  Block
typealias ShmSimplePassNoValue = ()->();//不传数据，只通知
typealias ShmSimplePassByValue = (Any)->();//简单的传个数据

class ShmDownloadTool: NSObject , MZDownloadManagerDelegate{
    
    @objc static let shareInstance = ShmDownloadTool();
    
    var download : MZDownloadManager?;
    
    var downloadTimer : DispatchSourceTimer?;
    
    var downloadFinished : ShmSimplePassByValue?;
   
    
    override init() {
        super.init();
        DispatchQueue.global().async {
            self.download = MZDownloadManager(session: "playtool", delegate: self, sessionConfiguration: nil, completion: nil);
        }
       
    }
    
    func downloadMusicFile(model : ShmCxMusicModel , block : @escaping ShmSimplePassByValue) {
        self.download?.downloadingArray.removeAll();
        self.downloadFinished = block;
        let basePath = NSHomeDirectory() + "/tmp/";
       
        let filePath = "\(model.songId).ccmz"
        
        if !FileManager.default.fileExists(atPath: basePath + filePath) {
            if let url = URL(string: model.songUrl) {
                self.download?.addDownloadTask(filePath, request: URLRequest(url: url), destinationPath: basePath);
            }
        }else{
            model.locationFile = filePath;
        }
   
    }
    
//
//    func addDownloadTime() {
//        self.stopDownloadTime();
//        self.downloadTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global());
//        if self.downloadTimer != nil {
//            self.downloadTimer?.schedule(deadline: .now(), repeating: .milliseconds(1000));
//            self.downloadTimer?.setEventHandler {
//                self.DownloadGrogress();
//
//                if !self.isDownLoad {
//                    self.isDownLoad = true;
//                }
//            }
//            self.downloadTimer?.resume()
//        }
//    }
//
//    func stopDownloadTime() {
//        self.downloadTimer?.cancel();
//    }
//
//
//    @objc func DownloadGrogress() {
//        if self.download?.downloadingArray.count == 0 {
//            self.stopDownloadTime();
//            return;
//        }
//    }
//
    
    func downloadRequestDidUpdateProgress(_ downloadModel: MZDownloadModel, index: Int) {
       
    }
    
    func downloadRequestDidPopulatedInterruptedTasks(_ downloadModel: [MZDownloadModel]) {
        
    }
    func downloadRequestDidFailedWithError(_ error: NSError, downloadModel: MZDownloadModel, index: Int) {
        if let block = self.downloadFinished {
            block(false);
        }
    }
    
    func downloadRequestFinished(_ downloadModel: MZDownloadModel, index: Int) {
        let filePath = downloadModel.fileName.replacingOccurrences(of: ".ccmz", with: "");
        let json = InsCcUnzipTool.ccmzToZip(filePath: downloadModel.destinationPath + downloadModel.fileName);
        if let block = self.downloadFinished {
            block(json);
        }
    }
    
    

    
}
